<template>
  <div>
    雪场列表
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>雪场列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="25">
        <!-- 搜索区域     -->
        <el-col :span="10">
          <el-input
            placeholder="请输入搜索内容"
            clearable
            @clear="getFieldList"
            v-model="queryInfo.query"
          >
            <template #append>
              <el-button
                icon="el-icon-search"
                @click="getFieldList"
              ></el-button>
            </template>
          </el-input>
        </el-col>
        <el-col :span="4">
          <!-- 下拉筛选 -->
          <el-select v-model="queryInfo.type" filterable placeholder="请选择城市">
            <el-option
              v-for="item in cities"
              :key="item.value"
              :label="item.label"
              :value="item.label"
              @click="getFieldList"
            >
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                item.value
              }}</span>
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <!-- 搜索按钮 -->
          <el-button type="primary" @click="addDialog">添加新场地</el-button>
        </el-col>
      </el-row>

      <!-- 场地列表 border边框，stripe隔行变色 -->
      <el-table :data="fieldList" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="fieldName" label="场地名称"> </el-table-column>
        <el-table-column prop="fieldLocation" label="场地地址">
        </el-table-column>
        <el-table-column prop="fieldPrice" label="门票价格"> </el-table-column>
        <el-table-column prop="fieldView" label="场地图片">
          <template v-slot="scope">
            <img width="290" height="200" :src="port+scope.row.fieldView" />
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template v-slot="scope">
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="updateVisible(scope.row)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteField(scope.row.fieldId)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页部分 size-change 每页最大变化 current-change 当前最大变化 layout功能组建-->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 5, 10, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增场地区域 -->
    <el-dialog
      title="添加新场地"
      v-model="addDialogVisible"
      width="50"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="addFormrules"
        ref="addFormRef"
        label-width="80px"
      >
        <el-form-item label="场地名称" prop="fieldName">
          <el-input v-model="addForm.fieldName"></el-input>
        </el-form-item>
        <el-form-item label="场地地址" prop="fieldLocation">
          <el-input v-model="addForm.fieldLocation"></el-input>
        </el-form-item>
        <el-form-item label="门票价格" prop="fieldPrice">
          <el-input v-model="addForm.fieldPrice"></el-input>
        </el-form-item>
        <el-form-item label="场地图片" prop="fieldView">
          <el-upload
            v-model="addForm.fieldView"
            class="avatar-uploader"
            action="http://localhost:9000/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="{ role: role }"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="port+imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addField">确定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 修改场地区域 -->
    <el-dialog
      title="修改场地"
      v-model="updateDialogVisible"
      width="50"
      @close="updateDialogClosed"
    >
      <el-form
        :model="updateForm"
        :rules="updateFormrules"
        ref="updateFormRef"
        label-width="80px"
      >
        <el-form-item label="场地名称" prop="fieldName">
          <el-input v-model="updateForm.fieldName"></el-input>
        </el-form-item>
        <el-form-item label="场地地址" prop="fieldLocation">
          <el-input v-model="updateForm.fieldLocation"></el-input>
        </el-form-item>
        <el-form-item label="门票价格" prop="fieldPrice">
          <el-input v-model="updateForm.fieldPrice"></el-input>
        </el-form-item>
        <el-form-item label="场地图片" prop="fieldView">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:9000/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="{ role: role }"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="port+imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="updateDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="updateField">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //图片上传
      imageUrl: "",
      role: "",
      port:"http://localhost:9000",
      //查询信息实体
      queryInfo: {
        query: "", //查询信息
        type:'',
        pageNum: 1, //当前页
        pageSize: 10, //每页的最大值
      },
      fieldList: [], //场地列表
      total: 0, //总记录数
      addDialogVisible: false, //对话框隐藏显示状态
      updateDialogVisible: false,
      //添加表单信息
      addForm: {
        fieldName: "",
        fieldLocation: "",
        fieldPrice: "",
        fieldView: "",
      },
      //更新表单信息
      updateForm: {
        fieldId: 0,
        fieldName: "",
        fieldLocation: "",
        fieldPrice: "",
        fieldView: "",
      },
      //下拉筛选城市
      cities: [
        
        {
          value: "显示全部",
          label: "",
        },
        {
          value: "Chengdu",
          label: "成都",
        },
        {
          value: "Beijing",
          label: "北京",
        },
        {
          value: "Shanghai",
          label: "上海",
        },
        {
          value: "Nanjing",
          label: "南京",
        },
        {
          value: "Shenzhen",
          label: "深圳",
        },
        {
          value: "Guangzhou",
          label: "广州",
        },
      ],
      value: "",
      //表单验证
      addFormrules: {
        fieldName: [
          { required: true, message: "请输入雪场名称", trigger: "blur" },
        ],
        fieldLocation: [
          { required: true, message: "请输入雪场地址", trigger: "blur" },
        ],
        fieldPrice: [
          { required: true, message: "请输入雪场门票价格", trigger: "blur" },
        ],
        fieldView: [
          { required: true, message: "请上传雪场图片", trigger: "blur" },
        ],
      },
      //修改表单验证
      updateFormrules: {
        fieldName: [
          { required: true, message: "请输入雪场名称", trigger: "blur" },
        ],
        fieldLocation: [
          { required: true, message: "请输入雪场地址", trigger: "blur" },
        ],
        fieldPrice: [
          { required: true, message: "请输入雪场门票价格", trigger: "blur" },
        ],
        fieldView: [
          { required: false, message: "请上传雪场图片", trigger: "blur" },

        ],
      },
    };
  },
  created() {
    this.getFieldList();
    this.role = encodeURIComponent(sessionStorage.getItem("role"));
  },
  methods: {
    //获取全部场地信息
    async getFieldList() {
      //调用post
      const { data: res } = await this.$http.get("allField", {
        params: this.queryInfo,
      });
      this.fieldList = res.data; //将返回数据赋值
      console.log(res.data);
      this.total = res.numbers;
    },
    //最大数
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getFieldList();
    },
    //pageNum的触发动作
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getFieldList();
    },
    //监听添加场地的操作
    addDialogClosed() {
      this.imageUrl = "";
      this.$refs.addFormRef.resetFields();
    },
    updateDialogClosed() {
      this.imageUrl = "";
      this.$refs.updateFormRef.resetFields();
    },
    addField() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("addField", this.addForm);
        if (res != "success") return this.$message.error("操作失败");
        this.$message.success("操作成功");
        this.addDialogVisible = false;
        this.getFieldList();
      });
    },
    addDialog() {
      this.addForm.fieldView = "";
      this.addDialogVisible = true;
    },
    //根据主键删除场地信息
    async deleteField(fieldId) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除场地，是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult != "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await this.$http.delete(
        "deleteField?fieldId=" + fieldId
      );
      if (res != "success") {
        return this.$message.error("删除失败");
      }
      this.$message.success("删除成功");
      this.getFieldList();
    },
    updateVisible(fieldInfo) {
      // console.log(fieldInfo)
      (this.updateDialogVisible = true),
        (this.updateForm.fieldName = fieldInfo.fieldName),
        (this.updateForm.fieldLocation = fieldInfo.fieldLocation),
        (this.updateForm.fieldPrice = fieldInfo.fieldPrice),
        (this.imageUrl = fieldInfo.fieldView),
        (this.updateForm.fieldId = fieldInfo.fieldId);
        this.updateForm.fieldView=fieldInfo.fieldView;
    },

    async updateField() {
      this.$refs.updateFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "updateField",
          this.updateForm
        );
        if (res != "success") return this.$message.error("操作失败");
        this.$message.success("操作成功");
        this.updateDialogVisible = false;
        console.log(this.imageUrl);
        this.updateForm.fieldView=this.imageUrl;
        this.getFieldList();
      });
    },
    handleAvatarSuccess(res, file) {
      console.log(res);
      this.imageUrl = res.filename;
      this.addForm.fieldView=this.imageUrl;
      console.log(this.imageUrl);
      this.updateForm.fieldView=this.imageUrl;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>
<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 17px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 290px;
  height: 200px;
  display: block;
}
.form-item {
  margin-left: 10px;
  text-align: center;
  vertical-align: middle;
  margin-bottom: 22px;
  margin-top: 22px;
}
.topbutton {
  margin-top: 22px;
}
</style>
